package com.mjh.zhujianju.controller.agency;

import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.pojo.LoginUserInfo;
import com.livew.framework.utils.XTCurrentUser;
import com.mjh.zhujianju.model.agency.DepartmentEO;
import com.mjh.zhujianju.service.agency.WorkDynamicService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.vo.agency.WorkDynamicVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liuyang
 * @Description: 工作动态
 * @date 2018/8/21 21:18
 */
@RestController
@RequestMapping("dynamic")
@Api("工作动态")
public class WorkDynamicController {

    @Autowired
    private WorkDynamicService workDynamicService;

    @ApiOperation(value="机构动态", notes="机构动态List")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getWorkDynamicList (){
        JsonResult result = new JsonResult();
        try {
            //获取当前登录用户信息
            //根据当前用户所属机构查询该机构下面所有的工作动态
        	LoginUserInfo info = XTCurrentUser.getCurrentUser();
            List<WorkDynamicVO> list = workDynamicService.queryWorkDynamicList(info.getDeptId());
            result.setResult(list);
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (Exception e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }


    @ApiOperation(value="添加动态", notes="用户添加机构动态信息")
    @RequestMapping(value = "/addDynamic", method = RequestMethod.POST)
    @ApiImplicitParam(name="动态实体类", value="WorkDynamicVO", required = true, dataType = "WorkDynamicVO")
    public ResponseEntity<JsonResult> releaseDynamic (@RequestBody WorkDynamicVO workDynamicVO){
        JsonResult result = new JsonResult();
        try {
            //Session获取当前登录用户信息
            //根据当前用户所属机构查询该机构下面所有的工作动态
        	LoginUserInfo info = XTCurrentUser.getCurrentUser();
            workDynamicVO.setCreateUserId(info.getDeptId());
            workDynamicService.saveInfo(workDynamicVO);
            result.setResult("发布成功");
            result.setStatus(ApiConstant.MSG_SUCCESS);
        }catch (XTBusinessException e){
            result.setResult(e.getClass().getName() + ":" + e.getMessage());
            result.setStatus(ApiConstant.MSG_ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(result);
    }




}
